﻿using LineFocus.Nikcron.Common;
using LineFocus.Nikcron.Models;
using Nickron.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LineFocus.Nikcron.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Manufacture()
        {
            ViewBag.Header = "Brands";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Products";
            return View();
        }

        public ActionResult ManufactureMaintenance()
        {
            return View();
        }

        #region Laptops
        [HttpGet]
        public ActionResult Laptops()
        {
            ViewBag.Header = "Laptops";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Products";
            return View();
        }
        [HttpGet]
        public JsonResult GetLaptops()
        {
            JsonResult Result = new JsonResult();
            ApplicationDBContext db = new ApplicationDBContext();
            Result.Data = db.ProductModels.OfType<Laptop>().Select(n => new
            {
                n.Id,
                Manufacture = n.Manufacture.Name,
                n.ModelName,
                n.ModelNumber,
                n.ProductColors,
                n.Processor,
                n.Chipset,
                n.ClockSpeed,
                n.Cache,
                n.Memory,
                n.ExternalStorage,
                n.HardwareInterface,
                n.RPM,
                n.InternalStorage,
                n.HDDCapacity,
                n.OpticalDrive,
                n.WarrantySummary,
                n.OperatingSystem,
                n.SystemArch,
                n.DisplaySize,
                n.DisplayType,
                n.Keyboard,
                n.Speakers,
                n.Sound,
                n.USBPort,
                n.RJ45LAN,
                n.HDMIPort,
                n.VGAPort,
                n.MultiCardSlot,
                n.Weight
            });
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }
        [HttpGet]
        public ActionResult LaptopMaintenance(Int32? Id)
        {
            Lookup Lookup_Manufactures = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Manufactures
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Manufactures.Items = query1.ToList();

            ViewBag.Header = "Laptop";
            ViewBag.Manufactures = Lookup_Manufactures;

            if (Id.HasValue)
            {
                ViewBag.Id = Id;
                ViewBag.Caption = "Edit";
                Laptop loptop = db.ProductModels.OfType<Laptop>().Where(l => l.Id == Id).FirstOrDefault();
                return View(loptop);
            }
            else
            {
                ViewBag.LaptopId = 0;
                ViewBag.Caption = "New";
                Laptop Model = new Laptop();
                Model.MRP.Amount = 0;
                return View(Model);
            }
        }
        [HttpPost]
        public ActionResult LaptopMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Laptop Laptop = new Laptop();

            Int32 LaptopId = 0;
            Int32.TryParse(formCollection["id"], out LaptopId);
            if (LaptopId == 0)
                Laptop = new Laptop();
            else

                Laptop = db.ProductModels.OfType<Laptop>().Where(l => l.Id == LaptopId).FirstOrDefault();
            Laptop.ProductTypeId = db.ProductTypes.Where(t => t.Name == "Laptop").FirstOrDefault().Id;
            Laptop.ManufactureId = int.Parse(formCollection["Manufacture"]);
            Laptop.ProductDate = DateTime.Parse(formCollection["ProductDate"]);
            Laptop.InBox = formCollection["InBox"];
            Laptop.ModelName = formCollection["ModelName"];
            Laptop.ModelNumber = formCollection["ModelNumber"];
            //Laptop.ProductColors = formCollection["ProductColors"];

            Laptop.Processor = formCollection["Processor"];
            Laptop.Chipset = formCollection["Chipset"];
            Laptop.ClockSpeed = formCollection["ClockSpeed"];
            Laptop.Cache = formCollection["Cache"];
            Laptop.ExternalStorage = formCollection["ExternalStorage"];
            Laptop.MemorySlots = formCollection["Memoryslote"];
            Laptop.Memory = formCollection["Systemmemory"];
            Laptop.HardwareInterface = formCollection["HardwareInterface"];
            Laptop.RPM = formCollection["RPM"];
            Laptop.HDDCapacity = formCollection["HDDCapacity"];
            Laptop.OpticalDrive = formCollection["OpticalDrive"];
            Laptop.NoteSummary = formCollection["NoteSummary"];
            Laptop.WarrantySummary = formCollection["WarrantySummary"];
            Laptop.OperatingSystem = formCollection["OperatingSystem"];
            Laptop.SystemArch = formCollection["SystemArch"];
            Laptop.DisplaySize = formCollection["DisplaySize"];
            Laptop.DisplayType = formCollection["DisplayType"];
            Laptop.GraphicProcessor = formCollection["GraphicProcessor"];
            Laptop.Keyboard = formCollection["Keyboard"];
            Laptop.Speakers = formCollection["Speakers"];
            Laptop.Sound = formCollection["Sound"];
            Laptop.USBPort = formCollection["USBport"];
            Laptop.RJ45LAN = formCollection["RJ45LAN"];
            Laptop.HDMIPort = formCollection["HDMIPort"];
            Laptop.VGAPort = formCollection["VGAport"];
            Laptop.MultiCardSlot = formCollection["MultiCardSlot"];
            Laptop.Dimension = formCollection["Dimension"];
            Laptop.Weight = formCollection["Weight"];
            if (!string.IsNullOrEmpty(formCollection["MRP"]))
            {
                Laptop.MRP.Currency = Currencies.INR.ToString();
                Laptop.MRP.Amount = Double.Parse(formCollection["MRP"]);
            }
            

            if (LaptopId == 0)
                db.ProductModels.AddObject(Laptop);
            db.SaveChanges();
            return View("Laptops");
        }
        #endregion

        #region Mobiles

        [HttpGet]
        public ActionResult Mobilephones()
        {
            ViewBag.Header = "Mobiles";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Products";
            return View();
        }

        [HttpGet]
        public JsonResult GetMobiles()
        {
            JsonResult Result = new JsonResult();
            ApplicationDBContext db = new ApplicationDBContext();
            Result.Data = db.ProductModels.OfType<Mobile>().Select(n => new
            {
                n.Id,
                Manufacture = n.Manufacture.Name,
                n.ModelNumber,
                n.ModelName,
                n.FormFactor,
                n.CallFeatures,
                n.SimSize,
                n.SimType,
                n.PrimaryCamera,
                n.SecondryCamera,
                n.PhoneMemory,
                n.VideoRecording,
                n.Dimension,
                n.Weight,
                n.BatteryType,
                n.BatteryStandBy,
                n.MusicPlayer,
                n.VideoPlayer,
                n.FM,
                n.SoundEnhancement,
                n.RingTone,
                n.WarrantySummary,
                n.Connectivity.OperatingFrequencies,
                n.OperatingSystem,
                n.Processor,
                n.DisplayType,
                n.DisplaySize,
                n.DisplayResolution,
                n.InternalStorage,
                n.ExternalStorage,
                n.Memory,
                n.InternetFeatures,
                n.PreinstalledBrowser,
                n.Connectivity.HSPA_3G,
                n.Connectivity.GPRS_2G,
                n.WiFi,
                n.Connectivity.USB,
                n.Connectivity.Bluetooth,
                n.Connectivity.SupportedNetworks,
                n.CallMemory,
                n.SMSMemory
            });
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }
        [HttpGet]
        public ActionResult MobilephoneMaintenance(Int32? Id)
        {
            Lookup Lookup_Manufactures = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Manufactures
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Manufactures.Items = query1.ToList();

            ViewBag.Header = "Mobiles";
            ViewBag.Manufactures = Lookup_Manufactures;

            if (Id.HasValue)
            {
                ViewBag.MobileId = Id;
                ViewBag.Caption = "Edit";
                Mobile Mobile = db.ProductModels.OfType<Mobile>().Where(m => m.Id == Id).FirstOrDefault();
                if (!string.IsNullOrEmpty(Mobile.NoteSummary))
                    Mobile.NoteSummary = Mobile.NoteSummary.Replace("\r\n", "<br/>");
                return View(Mobile);
            }
            else
            {
                ViewBag.MobileId = 0;
                ViewBag.Caption = "New";
                Mobile Model = new Mobile();
                Model.MRP.Amount = 0;
                return View();
            }
        }
        [HttpPost]
        public ActionResult MobilephoneMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Mobile MobilePhone = new Mobile();

            Int32 Id = 0;
            Int32.TryParse(formCollection["Id"], out Id);
            if (Id == 0)
                MobilePhone = new Mobile();
            else
                MobilePhone = db.ProductModels.OfType<Mobile>().Where(m => m.Id == Id).FirstOrDefault();

            MobilePhone.ProductTypeId = db.ProductTypes.Where(t => t.Name == "Mobile").FirstOrDefault().Id;
            MobilePhone.ManufactureId = int.Parse(formCollection["Manufacture"]);
            MobilePhone.ProductDate = DateTime.Parse(formCollection["ProductDate"]);
            MobilePhone.InBox = formCollection["InBox"];
            MobilePhone.ModelNumber = formCollection["ModelNumber"];
            MobilePhone.ModelName = formCollection["ModelName"];
            MobilePhone.FormFactor = formCollection["FormFactor"];
            MobilePhone.CallFeatures = formCollection["CallFeatures"];
            MobilePhone.SimSize = formCollection["SimSize"];
            MobilePhone.SimType = formCollection["SimType"];
            MobilePhone.PrimaryCamera = formCollection["PrimaryCamera"];
            MobilePhone.SecondryCamera = formCollection["SecondryCamera"];
            MobilePhone.VideoRecording = formCollection["VideoRecording"];
            MobilePhone.Dimension = formCollection["Dimension"];
            MobilePhone.Weight = formCollection["Weight"];
            MobilePhone.BatteryType = formCollection["BatteryType"];
            if (!string.IsNullOrEmpty(formCollection["BatteryStandBy"]))
                MobilePhone.BatteryStandBy = float.Parse(formCollection["BatteryStandBy"]);
            MobilePhone.BatteryCapacity = formCollection["BatteryCapacity"];
            MobilePhone.MusicPlayer = formCollection["MusicPlayer"];
            MobilePhone.VideoPlayer = formCollection["VideoPlayer"];
            MobilePhone.SoundEnhancement = formCollection["SoundEnhancement"];
            MobilePhone.RingTone = formCollection["RingTone"];
            MobilePhone.FM = formCollection["FM"];
            MobilePhone.WarrantySummary = formCollection["WarrantySummary"];
            //MobilePhone. = formCollection["ProductWarranties"];
            MobilePhone.Connectivity.OperatingFrequencies = formCollection["OperatingFrequencies"];
            MobilePhone.OperatingSystem = formCollection["OperatingSystem"];
            MobilePhone.Processor = formCollection["Processor"];
            MobilePhone.DisplayType = formCollection["DisplayType"];
            MobilePhone.DisplaySize = formCollection["DisplaySize"];
            MobilePhone.DisplayResolution = formCollection["DisplayResolution"];
            MobilePhone.InternalStorage = formCollection["InternalStorage"];
            MobilePhone.ExternalStorage = formCollection["ExternalStorage"];
            MobilePhone.PhoneMemory = formCollection["PhoneMemory"];
            MobilePhone.Memory = formCollection["Memory"];
            MobilePhone.InternetFeatures = formCollection["InternetFeatures"];
            MobilePhone.PreinstalledBrowser = formCollection["PreinstalledBrowser"];
            MobilePhone.Connectivity.HSPA_3G = formCollection["HSPA_3G"];
            MobilePhone.Connectivity.GPRS_2G = formCollection["GPRS_2G"];
            MobilePhone.Connectivity.EDGE_2G = formCollection["EDGE_2G"];
            MobilePhone.WiFi = formCollection["WiFi"];
            MobilePhone.Connectivity.Hotspot = formCollection["Hotspot"];
            MobilePhone.Connectivity.USB = formCollection["USB"];
            MobilePhone.Connectivity.Bluetooth = formCollection["Bluetooth"];
            MobilePhone.Connectivity.SupportedNetworks = formCollection["SupportedNetworks"];
            // MobilePhone.CallMemory = formCollection["CallMemory"];
            // MobilePhone.SMSMemory = formCollection["SMSMemory"];
            MobilePhone.NoteSummary = formCollection["NoteSummary"];
            if (!string.IsNullOrEmpty(formCollection["MRP"]))
            {
                MobilePhone.MRP.Currency = Currencies.INR.ToString();
                MobilePhone.MRP.Amount = Double.Parse(formCollection["MRP"]);
            }
            if (Id == 0)
                db.ProductModels.AddObject(MobilePhone);
            db.SaveChanges();
            return View("Mobilephones");
        }
        #endregion

        #region Tablets
        [HttpGet]
        public ActionResult Tablets()
        {
            ViewBag.Header = "Tablets";
            ViewBag.Caption = "Listing";
            ViewBag.Module = "Products";
            return View();
        }
        [HttpGet]
        public JsonResult GetTablets()
        {
            JsonResult Result = new JsonResult();
            ApplicationDBContext db = new ApplicationDBContext();
            Result.Data = db.ProductModels.OfType<Tablet>().Select(n => new
            {
                n.Id,
                Manufacture = n.Manufacture.Name,
                n.ModelNumber,
                n.ModelName,
                n.Processor,
                n.ProductColors,
                n.ExternalStorage,
                n.InternalStorage,
                n.Dimension,
                n.OperatingSystem,
                n.DisplaySize,
                n.DisplayType,
                n.PrimaryCamera,
                n.SecondryCamera,
                n.WiFi,
                n.PreinstalledBrowser,
                n.Weight,
                n.VideoFormat,
                n.VideoRecording,
                n.AudioFormat
            });
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }
        [HttpGet]
        public ActionResult TabletMaintenance(Int32? Id)
        {
            Lookup Lookup_Manufactures = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Manufactures
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Manufactures.Items = query1.ToList();

            ViewBag.Header = "Tablet";
            ViewBag.Manufactures = Lookup_Manufactures;

            if (Id.HasValue)
            {
                ViewBag.TabletId = Id;
                ViewBag.Caption = "Edit";
                Tablet Tablet = db.ProductModels.OfType<Tablet>().Where(t => t.Id == Id).FirstOrDefault();
                return View(Tablet);
            }
            else
            {
                ViewBag.TabletId = 0;
                ViewBag.Caption = "New";
                Tablet Model = new Tablet();
                Model.MRP.Amount = 0;
                return View(Model);
            }
        }
        [HttpPost]
        public ActionResult TabletMaintenance(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Tablet Tablet = new Tablet();

            Int32 Tabletid = 0;
            Int32.TryParse(formCollection["Id"], out Tabletid);
            if (Tabletid == 0)
                Tablet = new Tablet();
            else

                Tablet = db.ProductModels.OfType<Tablet>().Where(t => t.Id == Tabletid).FirstOrDefault();
            Tablet.ProductTypeId = db.ProductTypes.Where(t => t.Name == "Tablet").FirstOrDefault().Id;
            Tablet.ManufactureId = int.Parse(formCollection["Manufacture"]);

            Tablet.ProductDate = DateTime.Parse(formCollection["ProductDate"]);
            Tablet.InBox = formCollection["InBox"];
            Tablet.ModelName = formCollection["ModelName"];
            Tablet.ModelNumber = formCollection["ModelNumber"];
            Tablet.CallFeatures = formCollection["CallFeatures"];
            Tablet.Processor = formCollection["Processor"];
            // Tablet.ProductColors = formCollection["ProductColors"];
            Tablet.ExternalStorage = formCollection["ExternalStorage"];
            Tablet.InternalStorage = formCollection["InternalStorage"];
            Tablet.Dimension = formCollection["Dimension"];
            Tablet.Weight = formCollection["Weight"];
            Tablet.NoteSummary = formCollection["NoteSummary"];
            Tablet.WarrantySummary = formCollection["WarrantySummary"];
            Tablet.OperatingSystem = formCollection["OperatingSystem"];
            Tablet.DisplaySize = formCollection["DisplaySize"];
            Tablet.DisplayType = formCollection["DisplayType"];
            Tablet.PrimaryCamera = formCollection["PrimaryCamera"];
            Tablet.SecondryCamera = formCollection["SecondryCamera"];
            Tablet.PreinstalledBrowser = formCollection["PreinstalledBrowser"];
            Tablet.WiFi = formCollection["WiFi"];
            Tablet.Connectivity.USB = formCollection["USBPort"];
            Tablet.Connectivity.HSPA_3G = formCollection["HSPA_3G"];
            Tablet.Connectivity.GPRS_2G = formCollection["GPRS_2G"];
            Tablet.VideoFormat = formCollection["VideoFormat"];
            Tablet.VideoRecording = formCollection["VideoRecording"];
            Tablet.AudioFormat = formCollection["AudioFormat"];
            if (!string.IsNullOrEmpty(formCollection["MRP"]))
            {
                Tablet.MRP.Currency = Currencies.INR.ToString();
                Tablet.MRP.Amount = Double.Parse(formCollection["MRP"]);
            }
            if (Tabletid == 0)
                db.ProductModels.AddObject(Tablet);
            db.SaveChanges();
            return View("Tablets");
        }
        #endregion

        #region Accessories
        [HttpGet]
        public ActionResult Accessories()
        {
            return View();
        }
        [HttpGet]
        public JsonResult GetAccessories()
        { 
            JsonResult Result = new JsonResult();
            ApplicationDBContext db = new ApplicationDBContext();
            Result.Data = db.ProductModels.OfType<Accessory>().Select(n => new
            {
                n.Id,
                Manufacture = n.Manufacture.Name,
                n.ModelName,
                n.ModelNumber,
                AccessoryType = n.AccessoryType.Name,
                MRP = n.MRP.Amount
            });
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;


        }
        [HttpGet]
        public ActionResult AccessoryMaintenance(int? Id)
        {
            Lookup Lookup_Manufactures = new Lookup();
            Lookup Lookup_AccessoryTypes = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Manufactures
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Manufactures.Items = query1.ToList();

            var query2 = from records in db.AccessoryTypes
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_AccessoryTypes.Items = query2.ToList();

            ViewBag.Header = "Accessories";
            ViewBag.Manufactures = Lookup_Manufactures;
            ViewBag.AccessoryTypes = Lookup_AccessoryTypes;

            if (Id.HasValue)
            {
                ViewBag.Id = Id;
                ViewBag.Caption = "Edit";
                Accessory Accessory = db.ProductModels.OfType<Accessory>().Where(l => l.Id == Id).FirstOrDefault();
                return View(Accessory);
            }
            else
            {
                ViewBag.AccessoryId = 0;
                ViewBag.Caption = "New";
                Accessory Model = new Accessory();
                Model.MRP.Amount = 0;
                return View(Model);
            }
        }
        [HttpPost]
        public ActionResult AccessoryMaintenace(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            Accessory Accessory = new Accessory();

            Int32 AccessoryId = 0;
            Int32.TryParse(formCollection["id"], out AccessoryId);
            if (AccessoryId == 0)
                Accessory = new Accessory();
            else
                Accessory = db.ProductModels.OfType<Accessory>().Where(l => l.Id == AccessoryId).FirstOrDefault();

            Accessory.AccessoryTypeId = int.Parse(formCollection["AccessoryType"]);
            Accessory.ProductTypeId = db.ProductTypes.Where(t => t.Name == "Accessory").FirstOrDefault().Id;
            Accessory.ManufactureId = int.Parse(formCollection["Manufacture"]);
            Accessory.ProductDate = DateTime.Parse(formCollection["ProductDate"]);
            Accessory.InBox = formCollection["InBox"];
            Accessory.ModelName = formCollection["ModelName"];
            Accessory.ModelNumber = formCollection["ModelNumber"];
            Accessory.NoteSummary = formCollection["NoteSummary"];
            Accessory.WarrantySummary = formCollection["WarrantySummary"];
            Accessory.Dimension = formCollection["Dimension"];
            Accessory.Weight = formCollection["Weight"];
            if (!string.IsNullOrEmpty(formCollection["MRP"]))
            {
                Accessory.MRP.Currency = Currencies.INR.ToString();
                Accessory.MRP.Amount = Double.Parse(formCollection["MRP"]);
            }
            if (AccessoryId == 0)
                db.ProductModels.AddObject(Accessory);
            db.SaveChanges();
            return RedirectToAction("Accessories");
        }
        #endregion

        #region Cost Service
        [HttpGet]
        public JsonResult GetPriceDetailsByModelId(int? ModelId)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult Result = new JsonResult();
            if (db.ProductModels.Where(m => m.Id == ModelId).FirstOrDefault().MRP.Amount.HasValue)
                Result.Data = db.ProductModels.Where(m => m.Id == ModelId).FirstOrDefault().MRP.Amount.Value.ToString("f2");
            else
                Result.Data = "0.00";
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }
        [HttpGet]
        public JsonResult GetPriceDetailsByStockId(int? StockId)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult Result = new JsonResult();
            if (db.ProductItems.Where(m => m.Id == StockId).FirstOrDefault().UnitPrice.Amount.HasValue)
                Result.Data = db.ProductItems.Where(m => m.Id == StockId).FirstOrDefault().UnitPrice.Amount.Value.ToString("f2");
            else
                Result.Data = "0.00";
            Result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return Result;
        }
        #endregion
    }
}
